package cluedo;

import java.util.*;

public class Main {

	private static void pause() {
		// don't worry about what this method does.
		try {
			Thread.currentThread().sleep(1000);
		} catch(Exception e) {}
	}


	/**
     * Input a number from the keyboard. The number must be between the min and
     * max parameters.
     *
     * @param min
     * @param max
     * @param scanner
     * @return
     */
	private static int inputNumber(int min, int max, Scanner scanner) {
		while(true) {
			String x = scanner.nextLine();
			try {
				int answer = Integer.parseInt(x);
				if(answer >= min && answer <= max) {
					return answer;
				}
			} catch(NumberFormatException e) {
			}
			System.out.println("Invalid input!");
		}
	}

	private static boolean moveCharacter(Scanner s, Board b, Character c, int x, int y) {
		if(b.legalMove(x, y)) {
			Tile t = b.getTile(x, y);
			c.setPlace(x,y);
			
			if(t!=null) {
				Room r = t.getRoom();
				System.out.print(c.name() + " has entered the " + r.getName()+ "\n");
				boolean accusation = r.getName() == "Pool";
				
				System.out.print("Options:\n1)Make "+(accusation?"accusation":"suggestion")+"\n2)Continue");
				
				String action = s.nextLine();
				
				switch (action.charAt(0)){
				case '1': {
					makeAnnouncement(accusation, r, b, s);
				}
				default: return false;
				
				}
			}
			return true;
		}
		else{
			System.out.print("Cant move there\n");
			return false;
		}
	}

	public static void makeAnnouncement(boolean accusation, Room r, Board b, Scanner s) {
		
	}
	
	public static void playGame(Board board, Scanner scanner) {
		boolean finished=false;

		while(!finished) {
			for(Player player : board.getPlayers()) {
				int roll = new java.util.Random().nextInt(5) + 1;
				System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
				System.out.println("Player " + player.getName() + "'s Turn. Rolled a " + roll + "\n");
				for(Character character : player.getCharacters()) {
					System.out.println("Start move for " + character.name() + "\n");
					
					Tile t = board.getTile(character.x(), character.y());
					System.out.println(character.name() + " is"+ (t!=null?" in "+t.getRoom().getName():"") + " at x:" + character.x() + ", y:"+ character.y() + "\n");
		
					boolean skip = false;
					
					while(roll>0&&!skip) {
						
						System.out.print(roll+" moves remain\nOptions:\n1)Move South\n2)Move West\n3)Move North\n4)Move East\n");
						String action = scanner.nextLine();
						switch (action.charAt(0)){
						case '1': {
							if(moveCharacter(scanner, board, character, character.x(), character.y()+1))
								roll--;
							break;
						}
						case '2': {
							if(moveCharacter(scanner, board, character, character.x()-1, character.y()))
								roll--;
							break;
						}
						case '3': {
							if(moveCharacter(scanner, board, character, character.x(), character.y()-1))
								roll--;
							break;
						}
						case '4': {
							if(moveCharacter(scanner, board, character, character.x()+1, character.y()))
								roll--;
							break;
						}
						default: {
							System.out.print("Command not recognised\n");
							continue;
						}
						}
						
						
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		

		System.out.println("Starting a new game of Cluedo");
		System.out.println("-----------------------------");
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the number of players (3-6)");
		int pCount = inputNumber(3,6,scanner);

		Board b = new Board(pCount);

		playGame(b,scanner);
	}
}
